package ca.uvic.cs.cogz.reports;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.w3c.dom.Document;

import ca.uvic.cs.cogz.PromptUIPlugin;
import ca.uvic.cs.cogz.mapping.filter.TreeFilterManager;
import ca.uvic.cs.cogz.mapping.ontology.DataLoaderFacade;
import edu.stanford.smi.protege.util.SystemUtilities;
import edu.stanford.smi.protegex.prompt.plugin.PluginManager;

public abstract class AbstractReport {
	private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";

	public static List<AbstractReport> getReports() {
		List<AbstractReport> reports = new ArrayList<AbstractReport>();

		reports.add(new MappingStatusReport());
		reports.add(new MappingHierarchyReport(TreeFilterManager.SOURCE_SEARCH, DataLoaderFacade.getInstance().getSourceOntology()));
		reports.add(new MappingHierarchyReport(TreeFilterManager.TARGET_SEARCH, DataLoaderFacade.getInstance().getTargetOntology()));

		return reports;
	}

	public abstract String getReportTitle();

	public abstract Document getReportAsXML();

	public abstract String getReportAsHTML();

	protected String getCurrentDateTime() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);

		return sdf.format(cal.getTime());
	}

	protected String getHTMLStart() {
		StringBuilder htmlStart = new StringBuilder();
		htmlStart.append("<html><head>");
		htmlStart.append("<title>" + getReportTitle() + "</title>");

		htmlStart.append("<style type=\"text/css\" media=\"screen\">");
		htmlStart.append(getStyleSheet());
		htmlStart.append("</style>");

		htmlStart.append("</head>");
		htmlStart.append("<body>");

		return htmlStart.toString();
	}

	protected String getHTMLEnd() {
		StringBuilder htmlStart = new StringBuilder();
		htmlStart.append("</body></html>");

		return htmlStart.toString();
	}

	protected String getReportSummary() {
		StringBuilder report = new StringBuilder();

		report.append("<ReportSummary>");
		report.append("<ReportTitle>" + getReportTitle() + "</ReportTitle>");
		report.append("<ReportDate>" + getCurrentDateTime() + "</ReportDate>");
		report.append("<GeneratedBy>" + SystemUtilities.getUserName() + "</GeneratedBy>");
		report.append("</ReportSummary>");

		return report.toString();
	}

	protected String getReportSummaryAsHTML() {
		StringBuilder report = new StringBuilder();

		report.append("<div class=\"reporttitle\">" + getReportTitle() + "</div><br/>");
		report.append("<div class=\"reportsubtitle\">Generation Date: " + getCurrentDateTime() + "</div>");
		report.append("<div class=\"reportsubtitle\">Generated By: " + SystemUtilities.getUserName() + "</div>");

		return report.toString();
	}

	private String getStyleSheet() {
		StringBuffer styleSheet = new StringBuffer();
		try {
			BufferedReader in = new BufferedReader(new FileReader(PluginManager.getPromptPluginsDirectory() + PromptUIPlugin.class.getPackage().getName() + File.separatorChar + "reportstyle.css"));
			String line;
			while ((line = in.readLine()) != null) {
				styleSheet.append(line);
			}
			in.close();
		} catch (Exception e) {
			//  Auto-generated catch block
			e.printStackTrace();
		}

		return styleSheet.toString();
	}
}
